package com.easy.dn.utils.encryption;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


public final class EncryUtils {
    /**
     * 客户端私钥
     */
    private static final String CLIENT_PRIVATEKEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKbNojYr8KlqKD/y"
            + "COd7QXu3e4TsrHd4sz3XgDYWEZZgYqIjVDcpcnlztwomgjMj9xSxdpyCc85GOGa0"
            + "lva1fNZpG6KXYS1xuFa9G7FRbaACoCL31TRv8t4TNkfQhQ7e2S7ZktqyUePWYLlz"
            + "u8hx5jXdriErRIx1jWK1q1NeEd3NAgMBAAECgYAws7Ob+4JeBLfRy9pbs/ovpCf1"
            + "bKEClQRIlyZBJHpoHKZPzt7k6D4bRfT4irvTMLoQmawXEGO9o3UOT8YQLHdRLitW"
            + "1CYKLy8k8ycyNpB/1L2vP+kHDzmM6Pr0IvkFgnbIFQmXeS5NBV+xOdlAYzuPFkCy"
            + "fUSOKdmt3F/Pbf9EhQJBANrF5Uaxmk7qGXfRV7tCT+f27eAWtYi2h/gJenLrmtke"
            + "Hg7SkgDiYHErJDns85va4cnhaAzAI1eSIHVaXh3JGXcCQQDDL9ns78LNDr/QuHN9"
            + "pmeDdlQfikeDKzW8dMcUIqGVX4WQJMptviZuf3cMvgm9+hDTVLvSePdTlA9YSCF4"
            + "VNPbAkEAvbe54XlpCKBIX7iiLRkPdGiV1qu614j7FqUZlAkvKrPMeywuQygNXHZ+"
            + "HuGWTIUfItQfSFdjDrEBBuPMFGZtdwJAV5N3xyyIjfMJM4AfKYhpN333HrOvhHX1"
            + "xVnsHOew8lGKnvMy9Gx11+xPISN/QYMa24dQQo5OAm0TOXwbsF73MwJAHzqaKZPs"
            + "EN08JunWDOKs3ZS+92maJIm1YGdYf5ipB8/Bm3wElnJsCiAeRqYKmPpAMlCZ5x+Z"
            + "AsuC1sjcp2r7xw==";
    /**
     * 服务器端公钥
     */
    static final String SERVER_PUBLICKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCLCHnW224gxhLnf0ISB871gYy2C+9h+dkYkyxoaI/mqEGleXjnrq9" +
            "TZqJ6vlFQs77JRgwOAOBxB2gq1T911STBWj3BHvCsKwI+iVI7jSgN+3y6Ukadddy549ho3ybC+jZawjpq5hp8N51Oa+ON6FrVY0ANl3s3KkLJsAaaNJl7QIDAQAB";


    /**
     * description 客户端加密
     * param
     * return
     */
    public static Map<String, String> clientEncrypt(String value) {
        Map<String, String> map = new HashMap<>();
        String randomKey = RandomUtils.text(16);
        String data = AES.encrypt(value, randomKey);
        String key = RSA.encrypt(randomKey, SERVER_PUBLICKEY);
        map.put("key", key);
        map.put("value", data);
        return map;
    }

//    public static Map<String, String> text(String value) {
//        Map<String, String> map = new HashMap<>();
//        String randomKey = "3wlt2pvEyx9PUk1V";
//        String data = AES.encrypt("{\\\"vcode\\\":\\\"U62k\\\",\\\"phone\\\":\\\"13331690836\\\",\\\"type\\\":\\\"1\\\",\\\"area_Code\\\":\\\"86\\\",\\\"client_type\\\":\\\"0\\\"}\n", randomKey);
//        String key = RSA.encrypt(randomKey, SERVER_PUBLICKEY);
//        map.put("key", key);
//        map.put("value", data);
//        Logs.i(data+"\n");
//        Logs.i(key);
//        return map;
//    }

    public static String passEncrypt(String value) {
        return RSA.encrypt(value,SERVER_PUBLICKEY);
    }

    /**
     * description 客户端解密
     * param
     * return
     */
    public static String clientDecrypt(String easKey, String data) {
        // key解密
        String key = RSA.decrypt(easKey, CLIENT_PRIVATEKEY);
        // value 解密
        String value = AES.decrypt(data, key);
        return value;
    }

    public static String clientDecrypt( String data) {
        // key解密
        String key = RSA.decrypt(data, CLIENT_PRIVATEKEY);
        return key;
    }
    private EncryUtils() {
    }

    /**
     * description 根据算法加密
     * param 加密算法 加密内容
     * return 返回加密后字符串
     * author zhanqiulin
     * create 2017/8/6
     */
    public static String encry(Constants.Algorithm algorithm, String str) throws NoSuchAlgorithmException,
            UnsupportedEncodingException {
        // 生成一个MD5加密计算摘要
        MessageDigest md = MessageDigest.getInstance(algorithm.algorithm());
        // 计算md5函数
        md.update(str.getBytes(Constants.CHAR_ENCODING));
        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
        return byte2String(md.digest());
    }


    /**
     * 转换字节数组为16进制字串
     *
     * @param bByte
     * @return
     */
    private static String byte2String(byte[] bByte) {
        StringBuilder sBuffer = new StringBuilder();
        for (byte b : bByte) {
            sBuffer.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }
        return sBuffer.toString();
    }

}
